﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MapGenerator
{
    public partial class Load : Form
    {
        public List<List<int>> mapGrid;
        public Load()
        {
            InitializeComponent();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btFromClipBoard_Click(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Html))
            {
                tbMapData.Text = Clipboard.GetText(TextDataFormat.Html);
            }
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            string fileName = "";
            openFileDialog1.Filter = "Maps Files (.maps)|*.maps";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.InitialDirectory = Environment.CurrentDirectory;
            openFileDialog1.FileName = "map.maps";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
                string[] lines = System.IO.File.ReadAllLines(fileName);
                LoadText(lines);
            }
        }

        private void LoadText(string[] mapDataLines)
        {
            string line;
            int baseLength = 0;
            List<int> colList;
            mapGrid = new List<List<int>>();
            try
            {
                for (int i = 0; i < mapDataLines.Length; ++i)
                {
                    line = mapDataLines[i];
                    line = line.Replace(" ", string.Empty);
                    tbMapData.Text += line + "\r\n";
                    if ((i != 0) && (i != (mapDataLines.Length - 1)))
                    {
                        colList = new List<int>();
                        line = line.Substring(1);
                        if (i != (mapDataLines.Length - 2))
                        {
                            line = line.Remove(line.Length - 2);
                        }
                        else
                        {
                            line = line.Remove(line.Length - 1);
                        }
                        string[] data = line.Split(',');
                        if (data.Length == baseLength || baseLength == 0)
                        {
                            baseLength = data.Length;
                            foreach (string values in data)
                            {
                                colList.Add(int.Parse(values));
                            }
                            mapGrid.Add(colList);
                        }
                        else
                        {
                            string message = "Le fichier sélectionné n'est pas sous un bon format";
                            string caption = "Error Detected in Input";
                            MessageBoxButtons buttons = MessageBoxButtons.OK;

                            // Displays the MessageBox.
                            DialogResult result = MessageBox.Show(message, caption, buttons);

                            //Clean
                            mapGrid = new List<List<int>>();
                            tbMapData.Text = "";
                            return;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
                string message = "Le fichier sélectionné n'est pas sous un bon format";
                string caption = "Error Detected in Input";
                MessageBoxButtons buttons = MessageBoxButtons.OK;

                // Displays the MessageBox.
                DialogResult result = MessageBox.Show(message, caption, buttons);

                //Clean
                mapGrid = new List<List<int>>();
                tbMapData.Text = "";
                return;
            }
        }
    }
}
